import pageHelper from "./pageHelper"

export  function result_by_BM25(keywords,doc,total) {
  let result ={}
  for(let i =0;i<keywords.length;i++){
    let cur = doc[`${keywords[i]}`];
    if(cur==null){
      continue;
    }
    for (let j =0;j<cur.length;j++){
        let d =cur[j];
        let df = cur.length;
        let N  = total;
        let k1 = 1.5;
        let b = 0.75;
        let normalize = k1 *(1-b +b* Math.random()*2)
        let tf = d.freq
        let score = ((k1*tf)/(tf+normalize))*Math.log2((N-df+0.5)/(df+0.5));
        if(result[`${d.doc_id}`]==undefined){
          result[`${d.doc_id}`] =0;
        }
        result[`${d.doc_id}`] += score;
    }
  }
  let  keys = Object.keys(result);
  return {keys: keys,result: result};
}

export  function comprehension_result(result,info,pageNum,pageSize){
  let res = [];
  for(let i =0;i<info.length;i++){
    let score = Math.log2(info[i]["clickNum"])*0.75;
    result[`${info[i]["_id"]}`] += score;
    res.push({doc_id: info[i]["_id"],score: result[`${info[i]["_id"]}`]})
  }
  res.sort((a,b)=>{
    return b["score"] - a["score"];
  })
  res = pageHelper(pageNum,pageSize,res)
  return res;
}
